{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "第一题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2004-2-11 是这一年的第 42 天\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "# 输入日期\n",
    "year = int(input(\"请输入年份: \"))\n",
    "month = int(input(\"请输入月份: \"))\n",
    "day = int(input(\"请输入日期: \"))\n",
    "\n",
    "# 创建日期对象并计算是今年的第几天\n",
    "date = datetime(year, month, day)\n",
    "day_of_year = date.timetuple().tm_yday\n",
    "\n",
    "# 输出结果\n",
    "print(f\"{year}-{month}-{day} 是这一年的第 {day_of_year} 天\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "# 输出9x9乘法口诀表\n",
    "for i in range(1, 10):\n",
    "    for j in range(1, i+1):\n",
    "        print(f\"{j}*{i}={i*j}\", end=\"\\t\")\n",
    "    print()  # 每一行结束后换行\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的素数有 168 个，分别是：\n",
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def is_prime(n):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    for i in range(2, int(n**0.5) + 1):\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "# 查找1000以内的素数\n",
    "primes = [num for num in range(2, 1001) if is_prime(num)]\n",
    "\n",
    "# 输出结果\n",
    "print(f\"1000以内的素数有 {len(primes)} 个，分别是：\")\n",
    "print(primes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "45 = 3*3*5\n"
     ]
    }
   ],
   "source": [
    "def prime_factors(n):\n",
    "    factors = []\n",
    "    # 处理2的因数\n",
    "    while n % 2 == 0:\n",
    "        factors.append(2)\n",
    "        n //= 2\n",
    "    # 处理大于2的奇数因数\n",
    "    for i in range(3, int(n**0.5) + 1, 2):\n",
    "        while n % i == 0:\n",
    "            factors.append(i)\n",
    "            n //= i\n",
    "    # 如果剩余部分是质数\n",
    "    if n > 2:\n",
    "        factors.append(n)\n",
    "    return factors\n",
    "\n",
    "# 输入正整数\n",
    "num = int(input(\"请输入一个正整数: \"))\n",
    "\n",
    "# 获取质因数并输出\n",
    "factors = prime_factors(num)\n",
    "print(f\"{num} = {'*'.join(map(str, factors))}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该同学的成绩等级是: B\n"
     ]
    }
   ],
   "source": [
    "score = int(input(\"请输入成绩: \"))\n",
    "\n",
    "# 使用条件运算符嵌套判断等级\n",
    "grade = 'A' if score >= 90 else ('B' if score >= 60 else 'C')\n",
    "\n",
    "# 输出成绩等级\n",
    "print(f\"该同学的成绩等级是: {grade}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21342 是一个 5 位数，逆序输出为: 24312\n"
     ]
    }
   ],
   "source": [
    "num = int(input(\"请输入一个不多于6位的正整数: \"))\n",
    "\n",
    "# 求数字的位数\n",
    "num_str = str(num)\n",
    "length = len(num_str)\n",
    "\n",
    "# 逆序输出\n",
    "reversed_num = num_str[::-1]\n",
    "\n",
    "# 输出结果\n",
    "print(f\"{num} 是一个 {length} 位数，逆序输出为: {reversed_num}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母: 4, 空格: 0, 数字: 0, 其它字符: 0\n"
     ]
    }
   ],
   "source": [
    "# 输入字符串\n",
    "s = input(\"请输入一行字符串: \")\n",
    "\n",
    "# 初始化统计变量\n",
    "letters = 0\n",
    "spaces = 0\n",
    "digits = 0\n",
    "others = 0\n",
    "\n",
    "# 遍历字符串，统计各类字符\n",
    "for char in s:\n",
    "    if char.isalpha():\n",
    "        letters += 1\n",
    "    elif char.isdigit():\n",
    "        digits += 1\n",
    "    elif char == ' ':\n",
    "        spaces += 1\n",
    "    else:\n",
    "        others += 1\n",
    "\n",
    "# 输出结果\n",
    "print(f\"英文字母: {letters}, 空格: {spaces}, 数字: {digits}, 其它字符: {others}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果是: 5509641873278232\n"
     ]
    }
   ],
   "source": [
    "# 输入数字 a 和项数 N\n",
    "a = input(\"请输入一个数字: \")\n",
    "N = int(input(\"请输入项数: \"))\n",
    "\n",
    "# 计算 s = a + aa + aaa + aaaa + ...\n",
    "s = 0\n",
    "for i in range(1, N + 1):\n",
    "    s += int(a * i)\n",
    "\n",
    "# 输出结果\n",
    "print(f\"结果是: {s}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的完数有: [6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "def is_perfect(num):\n",
    "    # 求因子之和\n",
    "    divisors_sum = sum(i for i in range(1, num) if num % i == 0)\n",
    "    return divisors_sum == num\n",
    "\n",
    "# 查找并输出 1000 以内的完数\n",
    "perfect_numbers = [i for i in range(1, 1001) if is_perfect(i)]\n",
    "\n",
    "# 输出结果\n",
    "print(f\"1000以内的完数有: {perfect_numbers}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数列前 224 项的和为: 362.73919450465263\n"
     ]
    }
   ],
   "source": [
    "# 输入 N\n",
    "N = int(input(\"请输入 N: \"))\n",
    "\n",
    "# 初始化数列的前两项\n",
    "a, b = 2, 1\n",
    "sum_series = 0\n",
    "\n",
    "# 计算数列前 N 项的和\n",
    "for _ in range(N):\n",
    "    sum_series += a / b\n",
    "    # 更新 a 和 b 为斐波那契数列的下一项\n",
    "    a, b = a + b, a\n",
    "\n",
    "# 输出结果\n",
    "print(f\"数列前 {N} 项的和为: {sum_series}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 + 2! + 3! + ... + 12! 的值为: 522956313\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# 输入 N\n",
    "N = int(input(\"请输入 N: \"))\n",
    "\n",
    "# 计算 1 + 2! + 3! + ... + N!\n",
    "sum_factorials = sum(math.factorial(i) for i in range(1, N + 1))\n",
    "\n",
    "# 输出结果\n",
    "print(f\"1 + 2! + 3! + ... + {N}! 的值为: {sum_factorials}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "符合条件的整数是: 4256\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# 搜索符合条件的整数\n",
    "for x in range(1, 100000):\n",
    "    if math.isqrt(x + 100) ** 2 == x + 100 and math.isqrt(x + 368) ** 2 == x + 368:\n",
    "        print(f\"符合条件的整数是: {x}\")\n",
    "        break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       *\n",
      "      ***\n",
      "     *****\n",
      "    *******\n",
      "   *********\n",
      "  ***********\n",
      " *************\n",
      "***************\n",
      " *************\n",
      "  ***********\n",
      "   *********\n",
      "    *******\n",
      "     *****\n",
      "      ***\n",
      "       *\n"
     ]
    }
   ],
   "source": [
    "# 输入一个奇数 N\n",
    "N = int(input(\"请输入一个奇数 N: \"))\n",
    "\n",
    "# 打印上半部分\n",
    "for i in range(1, N+1, 2):  # i 从 1 到 N，步长为 2\n",
    "    print(' ' * ((N - i) // 2) + '*' * i)\n",
    "\n",
    "# 打印下半部分\n",
    "for i in range(N-2, 0, -2):  # i 从 N-2 到 1，步长为 -2\n",
    "    print(' ' * ((N - i) // 2) + '*' * i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 6 个月的兔子总数是: 8\n"
     ]
    }
   ],
   "source": [
    "# 输入月份数 N\n",
    "N = int(input(\"请输入月份数 N: \"))\n",
    "\n",
    "# 初始化兔子的数量，前两个月没有兔子繁殖\n",
    "a, b = 1, 1  # a 表示第一个月兔子的数量，b 表示第二个月兔子的数量\n",
    "for month in range(3, N+1):\n",
    "    a, b = b, a + b  # 每个月兔子的数量是前两个月的数量之和\n",
    "\n",
    "# 输出结果\n",
    "print(f\"第 {N} 个月的兔子总数是: {b}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的结果:  [1, 2, 3, 4, 5, 5, 5, 6, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "# 输入10个数\n",
    "numbers = [int(input(f\"请输入第 {i+1} 个数字: \")) for i in range(10)]\n",
    "\n",
    "# 对列表进行排序\n",
    "numbers.sort()\n",
    "\n",
    "# 输出排序结果\n",
    "print(\"排序后的结果: \", numbers)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵对角线元素之和为: 15\n"
     ]
    }
   ],
   "source": [
    "# 输入一个 3x3 矩阵\n",
    "matrix = []\n",
    "for i in range(3):\n",
    "    row = list(map(int, input(f\"请输入第 {i+1} 行的 3 个数字: \").split()))\n",
    "    matrix.append(row)\n",
    "\n",
    "# 计算对角线元素之和\n",
    "diagonal_sum = sum(matrix[i][i] for i in range(3))\n",
    "\n",
    "print(f\"矩阵对角线元素之和为: {diagonal_sum}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组: [3, 4, 5, 5, 6, 2334]\n"
     ]
    }
   ],
   "source": [
    "# 输入已排序的数组和待插入的数\n",
    "arr = list(map(int, input(\"请输入已排好序的数组（空格隔开）: \").split()))\n",
    "num = int(input(\"请输入要插入的数: \"))\n",
    "\n",
    "# 找到插入位置\n",
    "arr.append(num)  # 先将新数加到数组末尾\n",
    "arr.sort()  # 排序数组\n",
    "\n",
    "print(f\"插入后的数组: {arr}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "移动后的数组: [3214]\n"
     ]
    }
   ],
   "source": [
    "# 输入 n 个整数和 m\n",
    "n = int(input(\"请输入整数个数 n: \"))\n",
    "arr = list(map(int, input(f\"请输入 {n} 个整数（空格隔开）: \").split()))\n",
    "m = int(input(\"请输入要移动的位置 m: \"))\n",
    "\n",
    "# 将数组元素移动 m 个位置\n",
    "arr = arr[-m:] + arr[:-m]\n",
    "\n",
    "print(f\"移动后的数组: {arr}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第 4 号的人\n"
     ]
    }
   ],
   "source": [
    "# 输入人数 n\n",
    "n = int(input(\"请输入人数 n: \"))\n",
    "people = list(range(1, n+1))  # 初始化每个人的编号\n",
    "\n",
    "# 模拟报数过程\n",
    "index = 0\n",
    "while len(people) > 1:\n",
    "    index = (index + 2) % len(people)  # 每次跳过 2 人，报到 3 的人退出\n",
    "    people.pop(index)\n",
    "\n",
    "print(f\"最后留下的是原来第 {people[0]} 号的人\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "求和结果（偶数 n）: 0.5\n"
     ]
    }
   ],
   "source": [
    "def sum_even(n):\n",
    "    # 求 1/2 + 1/4 + ... + 1/n\n",
    "    return sum(1 / (2 * i) for i in range(1, n//2 + 1))\n",
    "\n",
    "def sum_odd(n):\n",
    "    # 求 1/1 + 1/3 + ... + 1/n\n",
    "    return sum(1 / (2 * i - 1) for i in range(1, (n//2) + 1))\n",
    "\n",
    "# 输入 n\n",
    "n = int(input(\"请输入一个整数 n: \"))\n",
    "\n",
    "if n % 2 == 0:\n",
    "    result = sum_even(n)\n",
    "    print(f\"求和结果（偶数 n）: {result}\")\n",
    "else:\n",
    "    result = sum_odd(n)\n",
    "    print(f\"求和结果（奇数 n）: {result}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
